/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
 *
 * @author Bruno
 * @author Tiago
 */
public class SendSMS implements Runnable {

    String number;
    String message;
    boolean resultado;
    
    public SendSMS(String number, String message) {
        this.number = number;
        this.message = message;
        Thread sms = new Thread(this);
        sms.start();
    }

    
    public void run() {

        sendSmsCmd();

    }

    /* ***********************************************************************
     * Envia sms
     * http://wiki.forum.nokia.com/index.php/How_to_Send_Text_SMS_in_Java_ME *
     * **********************************************************************/
    public boolean sendSmsCmd() {
        boolean result = true;
        try {
            //sets address to send message
            String addr = "sms://" + number;
            // opens connection
            MessageConnection conn = (MessageConnection) Connector.open(addr);
            // prepares text message
            TextMessage msg =
                    (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
            //set text
            msg.setPayloadText(message);
            // send message
            conn.send(msg);
            conn.close();
        } catch (SecurityException se) {
            // probably the user has not allowed to send sms
            // you may want to handle this differently
            result = false;
        } catch (Exception e) {
            result = false;
        }
        notify();
        return result;
    }
}